package com.cxm.weilai.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 路径工具包.
 *
 * @author admin
 */
public class FileUtil {

    /**
     * 获取路径.
     *
     * @param originalFilename String
     * @return String
     */
    public static String generateFilename(String originalFilename) {
        SimpleDateFormat dirSdf = new SimpleDateFormat("yyyyMM");
        String filePre = dirSdf.format(new Date());

        String fileExt = "";
        int lastIndex = originalFilename.lastIndexOf('.');

        if (lastIndex != -1) {
            fileExt = originalFilename.substring(lastIndex);
        }

        String filename = filePre + "/" + UUIDGenerator.getUUID() + fileExt;

        return filename;
    }

    /**
     * 图片获得字节码.
     *
     * @param file File
     * @return byte[]
     */
    public static byte[] getBytesFromFile(File file) {
        if (file == null) {
            return null;
        }
        try {
            FileInputStream stream = new FileInputStream(file);
            ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
            byte[] bytes = new byte[1000];
            for (int n; (n = stream.read(bytes)) != -1; ) {

                out.write(bytes, 0, n);

            }
            stream.close();
            out.close();
            return out.toByteArray();
        } catch (IOException ioException) {
        }
        return null;
    }

}
